package com.hw.dagl.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hw.dagl.model.Sys_Role;
import com.hw.dagl.model.Sys_User;
import com.hw.dagl.service.RoleService;
import com.hw.dao.utils.ControllerUtil;
import com.hw.dao.utils.Page;
import com.hw.interceptor.SystemLog;

@Controller
public class RoleController extends BaseController{

	@Resource
	private RoleService roleServiceImpl;
	
	
	/*
	 * 跳转到用户管理页�?
	 */
	@RequestMapping(method=RequestMethod.GET)
	public String roleHome(HttpServletResponse response){
		return "role/roleHome";
	}
	/*
	 * 分页查询角色信息
	 */
	@RequestMapping(method=RequestMethod.POST)
	public void findRoleByPage(HttpServletRequest request,HttpServletResponse response,Page<Sys_Role> pages, Sys_Role role){
		Page<Sys_Role> list = roleServiceImpl.findRole(pages,role);
		ControllerUtil.renderJson(response, list, "encoding:UTF-8","no-cache:false");
	}
	/*
	 * 显示�?��角色
	 */
	@RequestMapping(method=RequestMethod.POST)
	public void findRoleAll(HttpServletResponse response){
		List<Sys_Role> list = roleServiceImpl.findRoleAll();
		ControllerUtil.renderJson(response, list, "encoding:UTF-8","no-cache:false");
	}
	/*
	 * 根据角色ID 查询角色信息
	 */
	@RequestMapping(method=RequestMethod.POST)
	public void findRoleByRid(HttpServletRequest request,HttpServletResponse response,String rid){
		Sys_Role list = roleServiceImpl.findRoleByRid(rid);
		ControllerUtil.renderJson(response, list, "encoding:UTF-8","no-cache:false");
	}
	
	/*
	 * 批量删除角色
	 */
	@RequestMapping(method=RequestMethod.POST)
	public void delRole(HttpServletRequest request,HttpServletResponse response,String ids){
		boolean flag = roleServiceImpl.delRole(ids);
		ControllerUtil.renderJson(response, flag, "encoding:UTF-8","no-cache:false");
	}
	/*
	 * 自动生成角色ID
	 */
	@RequestMapping(method=RequestMethod.POST)
	public void findRoleRid(HttpServletRequest request,HttpServletResponse response){
		int flag = roleServiceImpl.findRoleRid();
		ControllerUtil.renderJson(response, flag, "encoding:UTF-8","no-cache:false");
	}
	/*
	 * 新增或修改角色信息
	 */
	@RequestMapping(method=RequestMethod.POST)
	public void saveRole(HttpServletRequest request,HttpServletResponse response,Sys_Role role,String jsyxqStr,String saveLx,String ids){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(!jsyxqStr.equals("")){
			try {
				role.setJsyxq(sdf.parse(jsyxqStr));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if(!saveLx.equals("")){//修改
			boolean flag = roleServiceImpl.updateRole(role);
			boolean flag2 = saveRoleMenu(role.getRid(),ids);
			ControllerUtil.renderJson(response, flag, "encoding:UTF-8","no-cache:false");
		}else{
			String rid = Integer.toString(roleServiceImpl.findRoleRid());
			role.setRid(rid);
			role.setCjsj(new Date());
			Sys_User user= (Sys_User)request.getSession().getAttribute("user");
			role.setCjr(user.getXm());
			boolean flag = roleServiceImpl.saveRole(role);
			boolean flag2 = saveRoleMenu(role.getRid(),ids);
			ControllerUtil.renderJson(response, flag, "encoding:UTF-8","no-cache:false");
		}
	}
	/*
	 * 保存角色目录信息
	 */
	public boolean saveRoleMenu(String rid,String ids){
		List<String> idList = new ArrayList<String>();
		if(StringUtils.isNotEmpty(ids)) {
			for (String s : StringUtils.split(ids, '|')) {
				idList.add(s);
			}
		}
		boolean flag = roleServiceImpl.saveRoleMenu(rid,idList);
		return flag;
	}
}
